package leetcode2;

public class Day7 {
    public static void main(String[] args) {
        System.out.println(Math.abs(10-90));
    }

    //1.


class ListNode {
    int val;
    ListNode next = null;
    public ListNode(int val) {
        this.val = val;
    }
}


    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     *
     */
    //反转链表，返回新的头结点
    private ListNode reverse(ListNode node) {
        //1.定义虚拟头结点
        //2.把结点头插到虚拟头结点后面
        ListNode newHead = new ListNode(0);
        ListNode cur = node;
        while(cur != null) {
            ListNode next = cur.next;
            cur.next = newHead.next;
            newHead.next = cur;
            cur = next;
        }
        return newHead.next;
    }
    public ListNode addInList (ListNode head1, ListNode head2) {
        // write code here
        //1.反转链表
        //2.高精度相加
        ListNode newHead1 = reverse(head1);
        ListNode newHead2 = reverse(head2);
        ListNode newHead = new ListNode(0);
        ListNode cur = newHead;
        int tmp = 0;
        while(newHead1!=null || newHead2!=null ) {
            if(newHead1!=null) {
                tmp += newHead1.val;
                newHead1 = newHead1.next;
            }
            if(newHead2!=null) {
                tmp += newHead2.val;
                newHead2 = newHead2.next;
            }
            ListNode node = new ListNode(tmp%10);
            cur.next = node;
            cur = cur.next;
            tmp/=10;
        }
        if(tmp!=0) {
            ListNode node = new ListNode(tmp);
            cur.next = node;
        }
        return reverse(newHead.next);
    }


}
